//
//  ViewController.m
//  DataCache
//
//  Created by zhengju on 16/6/30.
//  Copyright © 2016年 zhengju. All rights reserved.
//  下载请保留出处  https://github.com/zhengju/DataCache

#import "ViewController.h"

#import "ZJHttpRequestManager.h"

#import "ZJDataCache.h"

#import "ZJTeamCell.h"

#import "ZJTeamModel.h"


//屏幕高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
//屏幕高度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#define CellID @"ZJTeamCell"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    int _page;
}

@property(nonatomic,strong)ZJHttpRequestManager *manager;

@property(nonatomic,strong)NSMutableArray *dataList;

@property(nonatomic,weak)UITableView   *tableView;

@end

@implementation ViewController
/**
 *  @author zhengju, 16-06-30 18:06:50
 *
 *  @brief 懒加载
 *
 *  @return 返回数组
 */
- (NSMutableArray *)dataList{
    
    if (_dataList == nil) {
        
        _dataList = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataList;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _page = 1;
    
    [self configureTableView];
    
    [self downloadData];
    
}
/**
 *  @author zhengju, 16-06-30 18:06:17
 *
 *  @brief 初始化tableView
 */
- (void)configureTableView{
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight-20) style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    [tableView registerNib:[UINib nibWithNibName:CellID bundle:nil] forCellReuseIdentifier:CellID];
    
    
    [self.view addSubview:tableView];
    
    self.tableView = tableView;
    

}

-(void)jsonReadingWithData:(NSData *)data{
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *statusArray = dic[@"result"][@"data"][@"full"] ;
    
    NSLog(@"%@",statusArray);
    
    
    //   创建frame模型对象

    for (NSDictionary *dict  in statusArray) {
        
        ZJTeamModel *model = [[ZJTeamModel alloc]init];
    
        model.Title = dict[@"Title"];

        [self.dataList addObject:model];
    }
    // 赋值
    [self.tableView reloadData];
}

/**
 *  加载数据
 */
-(void)downloadData{
 
    
    NSString * path = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.1.1.05&__os__=android&_sport_t_=livecast&show_extra=1&f=livecast_id,LeagueType,status,Team1Id,Team2Id,Score1,Score2,MatchId,LiveMode,Discipline,data_from,Title,date,time,Team1,Team2,Flag1,Flag2,NewsUrl,VideoUrl,LiveUrl,LiveStatusExtra,VideoLiveUrl,VideoBeginTime,narrator,LeagueType_cn,Discipline_cn,comment_id,odds_id,VideoEndTime,if_rotate_video,LiveStatusExtra_cn,m3u8,android,period_cn,program,penalty1,media_tag,penalty1,Round_cn,extra,rec_ovx,VideoId&_sport_a_=typeMatches&team_id=nba_10";
    
 
    ZJDataCache *cache = [ZJDataCache sharedCache];
    NSData *data = [cache getDataWithStringName:path];
    if (data) {
        [self jsonReadingWithData:data];
    }else{//已经有缓存的话就不会走请求数据的接口
        self.manager = [[ZJHttpRequestManager sharedManager]initWithUrlString:path andBlock:^(ZJHttpRequestManager *manager) {
            
            //将数据存入缓存
            [cache saveDataWithData:manager.data andStringName:path];
            //取沙盒数据
            [self jsonReadingWithData:manager.data];
 
        }];
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ZJTeamCell * cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    
    cell.model = self.dataList[indexPath.row];
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

@end
